//
//  WebViewViewController.m
//  carcareIOS
//
//  Created by wr on 15/7/7.
//  Copyright (c) 2015年 baozun. All rights reserved.
//


#import "BaseWebViewController.h"
#import "NetDealMgr.h"
#import <NJKWebViewProgressView.h>
#import <NJKWebViewProgress.h>
#import "WebViewHandle.h"

#define TIMEOUT 15

@interface BaseWebViewController () <UIWebViewDelegate,NJKWebViewProgressDelegate,UIViewControllerBackButtonDataSource>{

    NJKWebViewProgressView    *_webViewProgressView; //  进度条的View
    NJKWebViewProgress        *_webViewProgress;     //  进度条对象
}

@property (nonatomic, copy) NSString *strUrl;

@end

@implementation BaseWebViewController

-(instancetype)initWithURL:(NSString *)URL{
    self = [super init];
    if (self) {
        self.strUrl = URL;
        self.isScaleBool = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak __typeof(self) wself = self;
    
    [self navSetupRightTitle:@"刷新" action:^{
        [wself reloadURL:wself.strUrl];
    }];
    
    NSArray *viewControllers = self.navigationController.viewControllers;
    if (viewControllers.count > 1) {
        [self navAddLeftImageName:@"close_icon" action:^{
            [wself.navigationController popViewControllerAnimated:YES];
            NSArray *viewControllers = wself.navigationController.viewControllers;
            for (NSInteger i = viewControllers.count - 1; i >= 0; i--) {
                UIViewController *vc = viewControllers[i];
                if (![vc isKindOfClass:[BaseWebViewController class]]) {
                    [wself.navigationController popToViewController:vc animated:YES];
                    break;
                }
            }
        }];
    }
    
    [self createWebView];
}

-(void)reloadURL:(NSString *)url{
    self.strUrl = url;
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT]];
}

- (void)createWebView
{
    [self reloadURL:self.strUrl];
    self.webView.scalesPageToFit = self.isScaleBool;
    [self.view addSubview:self.webView];
    
    // 创建进度条对象
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    self.webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.0f;
    CGRect barFrame = CGRectMake(0, 0, __MAIN_WIDTH, progressBarHeight);
    _webViewProgressView = [[NJKWebViewProgressView alloc]initWithFrame:barFrame];
    _webViewProgressView.progressBarView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_webViewProgressView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setIsScaleBool:(BOOL)isScaleBool{
    _isScaleBool = isScaleBool;
    self.webView.scalesPageToFit = isScaleBool;
}

/**
 *  点击返回上一层
 */
-(void)navClickOnBackItem{
    if (self.webView.canGoBack) {
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - NJKWebViewProgressDelegate
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress{
    [_webViewProgressView setProgress:progress animated:YES];
}

#pragma mark - webViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView{
 
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    __weak __typeof(self) wself = self;
#warning 监听title变化
    [NSTimer scheduledTimerWithTimeInterval:0.5 block:^{
        wself.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    } repeats:NO];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    [SCREEN debugAddMessage:[NSString stringWithFormat:@"%@",request.mainDocumentURL]];
    return [WebViewHandle handleURL:request.mainDocumentURL withViewController:self];
}



#pragma mark - setget
-(UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64)];
    }
    return _webView;
}

@end
